package controller

import (
	"github.com/gin-gonic/gin"
	"shop-bean/constant"
	"shop-bean/model"
	"shop-bean/vo"
	"shop-common/library/serializer"
	"shop-common/utils/contextArgs"
	"shop-common/utils/pagehelper"
	"shop-common/utils/queryWrapper"
	"shop-service/business"
)

type ProductController struct {
}

// @Tags 商品接口
// @description: 通过分组标签获取商品列表
// @Summary 通过分组标签id（tagId）获取商品列表
// @function: GetTopNoticeList
// @Accept  json
// @Produce  json
// @Param current query string false "当前页，默认1"
// @Param size query string false "每页大小，默认10"
// @Param tagId query int true "分组标签"
// @Success 200 {object} string "{"code":1," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /prod/prodByTagIdPage [get]
func (c *ProductController) ProdByTagIdPage(ctx *gin.Context) {
	form := pagehelper.SearchPageRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	tagId, err := contextArgs.GetUriIntQuery(ctx, "tagId")
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	paginator := pagehelper.New().SetOffset(&form).Paginator()
	page, err := business.ServiceImpl().ProductService.FindByTagIdPage(ctx, paginator, uint(tagId))
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	serializer.SuccessData(ctx, "", page)
}

// @Tags 商品接口
// @description: 通过分类id商品列表信息
// @Summary 根据分类ID获取该分类下所有的商品列表信息
// @function: GetTopNoticeList
// @Accept  json
// @Produce  json
// @Param current query string false "当前页，默认1"
// @Param size query string false "每页大小，默认10"
// @Param categoryId query int true "分类ID"
// @Success 200 {object} string "{"code":1," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /prod/page [get]
func (c *ProductController) Page(ctx *gin.Context) {
	form := pagehelper.SearchPageRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	categoryId, err := contextArgs.GetUriIntQuery(ctx, "categoryId")
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	paginator := pagehelper.New().SetOffset(form).SetFilter([]string{"prod_id"}).
		Push(queryWrapper.Eq("category_id", categoryId)).
		Push(queryWrapper.Eq("status", constant.GLOBAL_ENABLE)).
		Paginator()

	page, err := business.ServiceImpl().ProductService.FindPage(ctx, paginator)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	serializer.SuccessData(ctx, "", page)
}

// @Tags 商品接口
// @description: 商品详情信息
// @Summary 根据商品ID（prodId）获取商品信息
// @function: GetTopNoticeList
// @Accept  json
// @Produce  json
// @Param prodId query int true "商品ID"
// @Success 200 {object} string "{"code":1," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /prod/prodInfo [get]
func (c *ProductController) ProdInfo(ctx *gin.Context) {
	prodId, err := contextArgs.GetUriIntQuery(ctx, "prodId")
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	product, err := business.ServiceImpl().ProductService.FindById(prodId)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	if product == nil {
		serializer.SuccessData(ctx, "", nil)
		return
	}

	serializer.SuccessData(ctx, "", useFilterSkuList(product))

}

func useFilterSkuList(prod *model.Product) *vo.ProductVO {
	result := &vo.ProductVO{
		ProdId:             prod.ProdId,
		ProdName:           prod.ProdName,
		OriPrice:           prod.OriPrice,
		Price:              prod.Price,
		Brief:              prod.Brief,
		Content:            prod.Content,
		Imgs:               prod.Imgs,
		Status:             prod.Status,
		CategoryId:         prod.CategoryId,
		TotalStocks:        prod.TotalStocks,
		DeliveryMode:       prod.DeliveryMode,
		DeliveryTemplateId: prod.DeliveryTemplateId,
		SkuTags:            prod.SkuTags,
		CreatedAt:          prod.CreatedAt,
		UpdatedAt:          prod.UpdatedAt,
		//TagList:            prod.TagList,
		//SkuList:            nil,
	}

	if len(prod.SkuList) > 0 {
		for _, sku := range prod.SkuList {
			if sku.Status == 2 {
				result.SkuList = append(result.SkuList, &vo.SkuVO{
					SkuId:        sku.SkuId,
					Properties:   sku.Properties,
					OriPrice:     sku.OriPrice,
					Price:        sku.Price,
					Stocks:       sku.Stocks,
					ActualStocks: sku.ActualStocks,
					PartyCode:    sku.PartyCode,
					ModelId:      sku.ModelId,
					Imgs:         sku.Imgs,
					ProdName:     sku.ProdName,
					SkuName:      sku.SkuName,
					Weight:       sku.Weight,
					Volume:       sku.Volume,
					Status:       sku.Status,
					CreatedAt:    sku.CreatedAt,
					UpdatedAt:    sku.UpdatedAt,
				})
			}

		}
	}

	return result
}
